﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Data;
using System.Diagnostics;
using System.Collections.ObjectModel;
using EnvCommonUtility;


namespace DAL
{
    /// <summary>
    /// 树的操作
    /// </summary>
    public class TreeViewOperation
    {
        #region 获得数据库信息放入 TreeView 中(递归)

        /// <summary>
        /// 获得数据库信息放入 TreeView 中(递归)
        /// </summary>
        /// <param name="connectionStringName">ConnectionString Name</param>
        /// <param name="mainProjectEXE">主工程可执行文件名称</param>
        /// <param name="parentID">过滤 ID</param>
        /// <param name="currentNode">本结点</param>
        /// <param name="parentNode">父结点</param>
        public void AddTreeView(string parentID, TreeGroupModel currentNode, TreeGroupModel parentNode)
        {
            try
            {
                IWMDal dataOper = new IWMDal();
                DataView dataTree = dataOper.GetDataSet(
                    string.Format(IWMDal.QUERY_GET_TREENODEDATA_BYPARENTID, parentID),
                    null, "db_VWMSConnectionString", string.Format("{0}.exe", System.Windows.Forms.Application.ProductName)).Tables[0].DefaultView;
                //dataTree.RowFilter = string.Format("ParentID = {0}", parentID);

                foreach (DataRowView row in dataTree)
                {
                    TagModel tag = new TagModel()
                    {
                        ID = row["ID"].ToString(),
                        ParentID = row["ParentID"].ToString(),
                        Name = row["TreeName"].ToString(),
                        Address = row["TreeAddress"].ToString(),
                        IP = row["TreeIP"].ToString(),
                        Port = row["TreePort"].ToString(),
                        Status = row["TreeStatus"].ToString(),
                        Teacher = row["TreeTeacher"].ToString(),
                        Remark = row["TreeRemark"].ToString(),
                    };

                    TreeGroupModel node = new TreeGroupModel(row["TreeName"].ToString(), new ObservableCollection<ITreeGroup>())
                    {
                        Arg = tag,
                        Parent = parentNode,
                        TooltipText = tag.Name,
                        Level = parentNode == null ? 0 : parentNode.Level + 1, // 0: 根
                    };

                    currentNode.Leaves.Add(node);
                    AddTreeView(row["ID"].ToString(), node, node);
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        #endregion
    }
}

